#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

const int num = 10;

void Worker()
{
    int cnt = 10;
    while(cnt--)
    {
        printf("我是子进程，我的pid:%d  cnt: %d\n",getpid(),cnt);
        sleep(1);
    }

}

int main()
{
    for(int i = 0; i < num; i++)
    {
        pid_t id = fork();
        if(id < 0) break;
        if(id == 0)
        {
            Worker();
            exit(0);//让子进程退出
        }
        printf("我是父进程，子进程的id:%d\n",id);
        sleep(1);
    }
    sleep(10);
    return 0;
}


/*
int main()
{
    printf("before fork:I am a prcess, pid:%d  ppid%d\n",getpid(),getppid());
    pid_t id = fork();
    if(id < 0)return 1;//创建进程失败
    if(id == 0)
    {
        //子进程
    }
    else 
    {
        //父进程
    }
    printf("after fork:I am a prcess,pid:%d  ppid%d  id:%d\n",getpid(),getppid(),id);
    sleep(2);

    return 0;
}
*/


